//package com.zhuss.aop;
//
//import cn.hutool.extra.spring.SpringUtil;
//import com.zhuss.anno.LockEnable;
//import com.zhuss.anno.LockPluginEnum;
//import com.zhuss.appconfig.RedisProcessor;
//import com.zhuss.common.AbstractBo;
//import com.zhuss.lock.Lock;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.ObjectUtils;
//
//import javax.annotation.PostConstruct;
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.stream.Collectors;
//
//@Aspect
//@Component
//public class LockAspect {
//
//	Logger  LOGGER = LoggerFactory.getLogger(LockAspect.class);
//
//	private static final Map<String, Lock> LOCK_MAP = new ConcurrentHashMap<>();
//
//	@PostConstruct
//	public void initLockPlugin() {
//		LOCK_MAP.put(LockPluginEnum.REDIS.name(), SpringUtil.getBean(RedisProcessor.class));
//		System.out.println(1);
//	}
//
//	@Pointcut("@annotation(com.zhuss.anno.LockEnable)")
//	public void pointCut() {
//
//	}
//
//	@Before("pointCut()")
//	public void before(JoinPoint point) throws Throwable {
//		Signature sign = point.getSignature();
//		if (sign instanceof MethodSignature) {
//			LockItem lockItem =  init(point);
//			if (ObjectUtils.isEmpty(lockItem)) {
//				return;
//			}
//
//			Lock lock = LOCK_MAP.get(lockItem.getLockPlugin());
//			String key = LockKeyUtil.genKey(lockItem);
//			lock.lock(key, lockItem.getLockTime(), lockItem.getCurrentThreadId());
//			System.out.println("加锁：" + System.currentTimeMillis());
//			System.out.println("加锁：" + lockItem.getSystem() + ":" + lockItem.getModule() + ":" + lockItem.getAction() + ":" + lockItem.getLockTime() + ":" + lockItem.getServicePkId() + ":" + lockItem.getCurrentThreadId());
//		}
//	}
//
//	private LockItem init(JoinPoint point) {
//		Signature sign = point.getSignature();
//		Method method = ((MethodSignature) sign).getMethod();
//		LockEnable lockAnno = method.getAnnotation(LockEnable.class);
//		String lockPlugin = lockAnno.lockPlugin().name();
//		String system = lockAnno.system().name();
//		String module = lockAnno.module().name();
//		String action = lockAnno.action().name();
//		Long lockTime = lockAnno.lockTime();
//		Object[] args = point.getArgs();
//		String servicePkId;
//		List<? extends Object> objectList = Arrays.stream(args).filter(item -> item instanceof AbstractBo).collect(Collectors.toList());
//		if (CollectionUtils.isEmpty(objectList) || objectList.size() > 1) {
//			servicePkId = null;
//		} else {
//			AbstractBo bo = (AbstractBo) objectList.get(0);
//			servicePkId = bo.getServicePkId();
//		}
//		return new LockItem(lockPlugin, system, module, action, lockTime, servicePkId, Thread.currentThread().getId());
//	}
//
//	@After("pointCut()")
//	public void after(JoinPoint point) throws Throwable {
//		Signature sign = point.getSignature();
//		if (sign instanceof MethodSignature) {
//			LockItem lockItem =  init(point);
//			if (ObjectUtils.isEmpty(lockItem)) {
//				return;
//			}
//			Lock lock = LOCK_MAP.get(lockItem.getLockPlugin());
//			String key = LockKeyUtil.genKey(lockItem);
//			lock.unlock(key);
//			System.out.println("释放锁：" + System.currentTimeMillis());
//			System.out.println("释放锁：" + lockItem.getSystem() + ":" + lockItem.getModule() + ":" + lockItem.getAction() + ":" + lockItem.getLockTime() + ":" + lockItem.getServicePkId() + ":" + lockItem.getCurrentThreadId());
//		}
//	}
//
//}
//
